'use strict';
require('./index.css')
require('page/common/nav/index.js')
require('page/common/header/index.js')
var _mm = require('util/mm.js')
var _order = require('service/order-service.js')
var navSide = require('page/common/nav-side/index.js')
var template = require('./index.string')
var Pagination = require('util/pagination/index.js')

var page = {
    data: {
        listParam: {
            pageNum: 1, //当前页
            pageSize: 3 //每页的个数
        }
    },
    init: function () {
        this.onLoad()
    },
    onLoad: function () {
        //初始化左侧菜单
        navSide.init({
            name: 'order-list'
        });
        this.loadOrderList()
    },
    // 加载订单列表
    loadOrderList: function () {
        var _this = this,
            orderListHtml = '',
            $listCon = $('.order-list-con');
        $listCon.html('<div class="loading"></div>')
        _order.getOrderList(_this.data.listParam, function (res) {
            orderListHtml = _mm.renderHtml(template, res);
            $listCon.html(orderListHtml)
            // 加载分页信息
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
                pageNum         : res.pageNum, //第几页
                pages           : res.pages   //总页数
            })
        }, function (errMsg) {
            $listCon.html('<div class="err-tip">加载订单失败，请刷新重试</div>')
        })
    },
       //加载分页信息
       loadPagination : function(pageInfo){
        var _this = this;
        // this.pagination ? '' : (this.pagination = new Pagination())
        var pagination = new Pagination()
        pagination.render($.extend({},pageInfo,{
            container : $('.pagination'),  //放置分页的容器
            onSelectPage : function(pageNum){
                // this.data.listParam.pageNum 变了 重新发送数据请求   也可单独写一个点击出来
                _this.data.listParam.pageNum = pageNum
                _this.loadOrderList()
            }
        }))
    }



}

$(function () {
    page.init()
})